package cn.power.win.win_power.acitivity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import cn.power.win.win_power.R;
import me.jessyan.autosize.AutoSizeCompat;
import me.jessyan.autosize.internal.CustomAdapt;

/*
* 联系我们
* */
public class ContractActivity extends Activity implements CustomAdapt {

    private ImageView mIvBack;
    private LinearLayout ll_net;
    private LinearLayout mLlPone;
    private LinearLayout mLlMail;
    public static final String TEL ="0551-65278258";
    public static final int MY_PERMISSION_REQUEST_CALL_PHONE =2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contract);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        ll_net = (LinearLayout) findViewById(R.id.ll_net);
        ll_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent it = new Intent();
                it.setAction(Intent.ACTION_VIEW);
                it.setData(Uri.parse("http://www.win-power.com"));
                startActivity(it);

            }
        });
        mLlPone = (LinearLayout) findViewById(R.id.ll_phone);
        mLlPone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  Uri uri = Uri.parse("tel:0551-65278258");
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);*/


                //检查自身的权限，第一个参数是上下文，第二个参数是需要申请的权限的字符串数组；第三个参数为requestCode
                //如果不等于PERMISSION_GRANTED那么就去请求权限
                if(ContextCompat.checkSelfPermission(ContractActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(ContractActivity.this,new String[]{Manifest.permission.CALL_PHONE},MY_PERMISSION_REQUEST_CALL_PHONE);
                } else{
                   // System.out.println("这里");
                    callPhone();
                }


            }
        });
        mLlMail = (LinearLayout) findViewById(R.id.ll_mail);
        mLlMail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /* Uri uri = Uri.parse("mailto:jason@win-power.cn");
                 Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                 startActivity(intent); */
        /*       Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_EMAIL, "jason@win-power.cn");
                intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                intent.setType("text/plain");
                startActivity(intent);*/
            }
        });

        //返回的点击事件
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到主界面
                startActivity(new Intent(ContractActivity.this,MainActivity.class));
            }
        });
    }

    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + TEL);
        intent.setData(data);
        startActivity(intent);

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == MY_PERMISSION_REQUEST_CALL_PHONE)
        {
            if (grantResults[0]==PackageManager.PERMISSION_GRANTED) {
             //   System.out.println("哪里");
                callPhone();
            } else {
                //Permission Denied
                Toast.makeText(ContractActivity.this,"拨打电话权限拒绝", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    public boolean isBaseOnWidth() {
        return false;
    }

    @Override
    public float getSizeInDp() {
        return 667;
    }
    @Override
    public Resources getResources() {
        //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
       // AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity((super.getResources()), 667, false);//如果有自定义需求就用这个方法
        return super.getResources();
    }
}
